/*
	Copyright (c) 2010, Mathias Myrland, Myrland Systemutvikling (mathias@myrsys.no)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	      
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	      
	    * Neither the name of Myrland Systemutvikling nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL Myrland Systemutvikling BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CGF_SIGNAL_HPP
#define CGF_SIGNAL_HPP

#include <string>

#include <boost/function.hpp>
#include <boost/signal.hpp>
#include <boost/unordered_map.hpp>
#include <boost/weak_ptr.hpp>

#include <cgf/object.hpp>

#include <iostream>


namespace CGF
{
	
	/**
	*	The SignalProducer class provides signal slots and means of connecting to them. The template parameter represents the type
	*	of the object that will be invoking the signals (in most cases this type is derived from SignalProducer).
	*
	*	@tparam	t_base	The type of the class that will be invoking the signals.	*/
	template<class t_base>
	class SignalProducer : public Object<t_base>
	{
			
		
		public:
			
			typedef boost::shared_ptr<t_base> ptr;
		
			/**
			*	Connect a function to a signal. If the signal does not exist, it is created
			*	
			*	@param	key	The name of the signal we want to connect with func
			*	@param	func	A boost::function object representing the callback function	*/
			boost::signals::connection connect ( std::string key, boost::function<void(ptr)> func )
			{
				if ( !m_eventSlots.count (key ) )
				{
					boost::shared_ptr<boost::signal<void(ptr)> > c ( new boost::signal<void(ptr)> );
					m_eventSlots[key] = c;
				}
						
				return m_eventSlots[key]->connect ( func );
			}
			
			/**
			*	Signal the specified slot with the provided producer as the source
			*
			*	@param	key	The id of the slot to signal	*/
			void signal ( std::string key )
			{
				
				boost::shared_ptr<t_base> src = this->_this().lock();
				//	Forward the call
				signal ( key, src );
			}
			/**
			*	Signal the specified slot with the provided producer as the source
			*
			*	@param	src	The object that will be regarded the caller of the signal
			*	@param	key	The id of the slot to signal	*/
			void signal ( std::string key, boost::shared_ptr<t_base> src )
			{
				if ( m_eventSlots.count ( key ) )
					(*m_eventSlots[key])( src );
			}
			
			SignalProducer()
			{
			}
			~SignalProducer ()
			{
				//	Signal the destruction signal
				this->signal ( "destroyed" );
			}
			
		private:
			/**	This unordered map maps names to event slots that can be connected to		*/
			boost::unordered_map<std::string, boost::shared_ptr<boost::signal<void(ptr)> > > m_eventSlots;
	};
}//	namespace CGF

#endif
